#############################################################################
#	Makefile for libmdm
#############################################################################
### set up compiler environment ###


LINK		= $(CC)
TARGET		= libcmsmdm.so

CFLAGS += -fPIC
CFLAGS += -DRDM_LATEST_SUPPORT 
CFLAGS += -DXML_LOCK_SUPPORT
CFLAGS += -DCUSTOM_PRECONFIG_SUPPORT
ifeq ($(GC_APPS_DBUS_SERVER),y)
CFLAGS += -DDBUS_OBJECT_NOTIFY
endif

LIBLZMA = LzmaEnc.o LzmaDec.o LzFind.o LzmaUtil.o


CFLAGS += -I.
CFLAGS += -Iutils
CFLAGS += -I$(CMS_BASE_DIR)/xml-tool/include
LDFLAGS += -Wl,-Map,$(TARGET).map

####### Files

HEADERS = $(wildcard ./*.h)
SOURCES = $(wildcard ./*.c)
OBJECTS = $(patsubst %.c, %.o, $(SOURCES))

LOG_HEADERS = $(wildcard ./log/*.h)
LOG_SOURCES = $(wildcard ./log/*.c)
LOG_OBJECTS = $(patsubst %.c, %.o, $(LOG_SOURCES))
HEADERS += $(LOG_HEADERS)
OBJECTS += $(LOG_OBJECTS)

MSG_HEADERS = $(wildcard ./msg/*.h)
MSG_SOURCES = $(wildcard ./msg/*.c)
MSG_OBJECTS = $(patsubst %.c, %.o, $(MSG_SOURCES))
HEADERS += $(MSG_HEADERS)
OBJECTS += $(MSG_OBJECTS)

UTI_HEADERS = $(wildcard ./utils/*.h)
UTI_SOURCES = $(wildcard ./utils/*.c)
UTI_OBJECTS = $(patsubst %.c, %.o, $(UTI_SOURCES))
HEADERS += $(UTI_HEADERS)
OBJECTS += $(UTI_OBJECTS)


EXTRAFILE = LzmaEnc.c LzmaDec.c LzFind.c LzmaUtil.c  xmltok.c xmlrole.c xmlparse.c
EXTRAFILE_OBJS = $(patsubst %.c, %.o, $(EXTRAFILE))
ifeq ($(wildcard  $(firstword $(EXTRAFILE_OBJS))),)
OBJECTS += $(EXTRAFILE_OBJS)
endif

OBJS += $(wildcard *.o)

.NOTPARALLEL: all
all: copyfile $(TARGET)
	cp $(TARGET) $(CMS_LIB_DIR)
	cp $(HEADERS) $(CMS_INC_DIR)
	@echo "###################################"
	@echo "#        $(TARGET) "
	@echo "#        Build up successfully"
	@echo "###################################"		

$(TARGET): $(OBJECTS) $(OBJS)
	$(LINK) $(CFLAGS) -shared $^ -o $@ $(LDFLAGS)

#copy need c file
copyfile:
	for file in $(EXTRAFILE);\
	do\
		if [ ! -f $${file} ]; then\
			find $(CMS_BASE_DIR) -name $${file} | head -1 | xargs -i cp {} ./;\
		fi;\
	done;

$(OBJECTS):%.o:%.c
	@#printf "  CC      $(*).c\n"
	$(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@	

clean:
	@echo "###################################"
	@echo "#        clean $(TARGET) "
	@echo "###################################"		
	rm -f $(EXTRAFILE)
	rm -rf *.so *.o *.d *.map $(TARGET) $(OBJECTS)
